<?php

namespace App\Controller;

// +----------------------------------------------------------------------
// | 文件 AjaxTopController.class.php
// +----------------------------------------------------------------------
// | 说明 获取推荐相关数据
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

/**
 * @apiDefine ApiErrorExample
 *
 * @apiErrorExample 失败返回
 * HTTP/1.1 200 OK
 * {
 *  "code": "",
 *  "info": "",
 *  "data": "",
 * }
 *
 */

/**
 * @apiDefine ApiSuccessExample
 *
 * @apiSuccessExample 成功返回
 * HTTP/1.1 200 OK
 * {
 *  "data": "",
 *  "info": "",
 *  "code": 200,
 * }
 *
 */

class AjaxTopController extends AjaxAppController {

	/**
	 * @api {get}  /app/AjaxTop/dailyRecommand 获取精选推荐的数据
	 * @apiVersion 4.0.0
	 * @apiName dailyRecommand
	 * @apiGroup AjaxTop
	 *
	 * @apiParam {int} per 个数
	 * @apiParam {bool} random 是否随机处理
	 *
	 * @apiSuccess {String} info  show message
	 * @apiSuccess {Number} status  status code.
	 * @apiSuccess {Object} data  extend datas.
	 * @apiSuccess {list} data.lists  列表数据 listsObject参考video数据
	 *
	 *
	 * @apiUse ApiSuccessExample
	 *
	 * @apiUse ApiErrorExample
	 */
	public function dailyRecommand($per = 8, $random = 0) {
		$videos = [];
		if (0 == $random) {
			$page = 1;
			$dailys = event('Video')->getDailyRecommentVids($page, $per);
			$res = array_column($dailys, 'video_id');
		} else {
			$ids = service('VideoDailyRecomment')->getField('video_id', true);
			$ids = array_unique($ids);
			shuffle($ids);
			$res = array_slice($ids, 0, $per);
		}
		foreach ($res as &$video_id) {
			$videos[] = event('Video')->detail($video_id);
		}
		$data['lists'] = $videos;
		return $this->response(1, $data);
	}

}
